package 队列与栈;

import java.util.Stack;

//最小栈
//https://leetcode.cn/problems/min-stack/description/
class 最小栈 {
    Stack<Integer> st = new Stack<>();
    Stack<Integer> minStack = new Stack<>();

    public 最小栈() {

    }

    public void push(int val) {
        st.push(val);
        // 维护 minStk 栈顶为全栈最小元素
        if (minStack.isEmpty() || val <= minStack.peek()) {
            // 新插入的这个元素就是全栈最小的
            minStack.push(val);
        } else {
            // 插入的这个元素比较大
            minStack.push(minStack.peek());
        }
    }

    public void pop() {
        minStack.pop();
        st.pop();
    }

    public int top() {
        return st.peek();
    }

    public int getMin() {
        return minStack.peek();
    }
}

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(val);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.getMin();
 */
